package com.laoli.lab40;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
 * delayedExecutor()示例
 */
public class DelayedExecutorDemo {
    public static void main(String[] args) throws InterruptedException {
        // 延迟executor
        Executor delayedExecutor = CompletableFuture.delayedExecutor(3000, TimeUnit.MILLISECONDS);

        System.out.printf("【主线程】提交时间:%s\n", LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));

        // 异步任务使用延迟executor执行
        CompletableFuture.runAsync(() -> {
            System.out.printf("【异步任务】执行时间:%s\n", LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        }, delayedExecutor);

        // 主线程休眠
        Thread.sleep(5000);
    }
}
